package com.stylefeng.guns.modular.biz.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.baomidou.mybatisplus.enums.SqlLike;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.toolkit.StringUtils;
import com.stylefeng.guns.core.base.controller.BaseController;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import com.stylefeng.guns.core.log.LogObjectHolder;
import org.springframework.web.bind.annotation.RequestParam;

import com.stylefeng.guns.modular.system.jqgrid.CustomPage;
import com.stylefeng.guns.modular.system.jqgrid.ExcelUtil;
import com.stylefeng.guns.modular.system.jqgrid.ExportPage;
import com.stylefeng.guns.modular.system.jqgrid.FrontPage;
import com.stylefeng.guns.modular.system.model.ItemBrand;
import com.stylefeng.guns.modular.biz.service.IItemBrandService;

/**
 * 品牌管理控制器
 *
 * @author fengshuonan
 * @Date 2018-09-28 18:09:42
 */
@Controller
@RequestMapping("/itemBrand")
public class ItemBrandController extends BaseController {

    private String PREFIX = "/biz/itemBrand/";

    @Autowired
    private IItemBrandService itemBrandService;


    /**
     * 跳转到品牌管理首页
     */
    @RequestMapping("")
    public String index(Model model) {
        return PREFIX + "itemBrand.html";
    }

    /**
     * 跳转到添加品牌管理
     */
    @RequestMapping("/itemBrand_add")
    public String itemBrandAdd() {
        return PREFIX + "itemBrand_add.html";
    }

    /**
     * 跳转到修改品牌管理
     */
    @RequestMapping("/itemBrand_update/{itemBrandId}")
    public String itemBrandUpdate(@PathVariable String itemBrandId, Model model) {
        ItemBrand itemBrand = itemBrandService.selectById(itemBrandId);
        model.addAttribute("item",itemBrand);
        LogObjectHolder.me().set(itemBrand);
        return PREFIX + "itemBrand_edit.html";
    }

    /**
     * 获取品牌管理列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(String condition) {
        return itemBrandService.selectList(null);
    }

    /**
     * 获取品牌管理列表(jqgrid)
     */
	@RequestMapping(value="/list2")
	@ResponseBody
	public Object list2(FrontPage<ItemBrand> page, HttpServletRequest request) {
		Page<ItemBrand> pageList = itemBrandService.selectPage(page.getPagePlus(),page.getWrapper());
		CustomPage<ItemBrand> customPage = new CustomPage<ItemBrand>(pageList);
		return customPage;
	}

    /**
   	 *导出Excel(jqgrid)
     */
	@ResponseBody
	@RequestMapping(value="/Export2Excel")
	//public Object Export2Excel(@RequestBody ExportPage<ItemBrand> page, HttpServletResponse response) {
	public void Export2Excel(HttpServletRequest req,  HttpServletResponse res) {
		 String params = req.getParameter("export2excel_params");
		//json字串转POJO
		ExportPage<ItemBrand> page = JSON.parseObject(params, new TypeReference<ExportPage<ItemBrand>>(){});
		List<Map<String, Object>> list = itemBrandService.selectMaps(page.getWrapper());
        Map<String,String> map = new HashMap<String, String>();
		try {
			ExcelUtil<ItemBrand> export = new ExcelUtil<ItemBrand>();
			export.exportToExcel(res, list, page);
		} catch (Exception e) {
            e.printStackTrace();
		}
	}    	
	
    /**
   	 *导出Excel(jqgrid)
	@ResponseBody
	@RequestMapping(value="/Export2Excel", method = RequestMethod.GET)
	public Object Export2Excel(HttpServletResponse response, @RequestBody ExportPage<ItemBrand> page) {
		List<Map<String, Object>> list = itemBrandService.selectMaps(page.getWrapper());
        Map<String,String> map = new HashMap<String, String>();
		try {
			ExcelUtil<ItemBrand> export = new ExcelUtil<ItemBrand>();
			export.exportToExcel(response, list, page);
		} catch (Exception e) {
	        map.put("code", "99");
	        map.put("message", "导出失败");					
			throw e;
		}
        map.put("code", "0");
        map.put("message", "导出成功");		
		return map; 		
	}    	
     */
	
    /**
     * 新增品牌管理
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(ItemBrand itemBrand) {
        itemBrandService.insert(itemBrand);
        return SUCCESS_TIP;
    }

    /**
     * 删除品牌管理
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam String itemBrandId) {
        itemBrandService.deleteById(itemBrandId);
        return SUCCESS_TIP;
    }

    /**
     * 修改品牌管理
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(ItemBrand itemBrand) {
        itemBrandService.updateById(itemBrand);
        return SUCCESS_TIP;
    }

    /**
     * 品牌管理详情
     */
    @RequestMapping(value = "/detail/{itemBrandId}")
    @ResponseBody
    public Object detail(@PathVariable("itemBrandId") String itemBrandId) {
        return itemBrandService.selectById(itemBrandId);
    }
}
